include("shared.lua")

function ENT:Initialize()
	self:SetModel("models/weapons/w_missile_closed.mdl")
	self.Emitter = ParticleEmitter(self:GetPos())
	self.Emitter:SetNearClip(16, 22)

	self.FireSound = CreateSound(self, "Missile.Ignite")
end

function ENT:Think()
	self.FireSound:PlayEx(0.85, 135 + math.sin(CurTime()*5))
	self.Emitter:SetPos(self:GetPos())
end

function ENT:OnRemove()
	self.FireSound:Stop()
	self.Emitter:Finish()
end

local matGlow = Material("sprites/light_glow02_add")
function ENT:Draw()
	local vOffset = self:GetPos()
	local vel = self:GetVelocity()
	if 10 < vel:Length() then
		self:SetAngles(vel:Angle())
	end

	local pos1 = self:GetPos()
	render.SetMaterial(matGlow)
	local r,g,b = self:GetColor()
	render.DrawSprite(pos1, 92 * SCALE, 92 * SCALE, Color(r,g,b))
	render.DrawSprite(pos1, 64 * SCALE, 64 * SCALE, color_white)

	local emitter = self.Emitter
	for i=1, 2 do
		particle = emitter:Add("sprites/light_glow02_add", pos1 + math.Rand(2, 6) * SCALE * VectorRand():Normalize())
		particle:SetDieTime(math.Rand(0.6, 1))
		particle:SetStartAlpha(230)
		particle:SetEndAlpha(50)
		particle:SetStartSize(math.Rand(28, 35) * SCALE)
		particle:SetEndSize(0)
		particle:SetRoll(math.Rand(0, 360))
		particle:SetRollDelta(math.Rand(-8, 8))
		particle:SetColor(r, g, b)

		particle = emitter:Add("effects/muzzleflash2", pos1)
		particle:SetVelocity(math.Rand(16, 32) * SCALE * VectorRand():Normalize())
		particle:SetDieTime(math.Rand(1.6, 2))
		particle:SetStartAlpha(230)
		particle:SetEndAlpha(0)
		particle:SetStartSize(math.Rand(20, 28) * SCALE)
		particle:SetEndSize(16 * SCALE)
		particle:SetRoll(math.Rand(0, 360))
		particle:SetRollDelta(math.Rand(-2.8, 2.8))
		particle:SetColor(r, g, b)
		particle:SetAirResistance(10)

		particle = emitter:Add("particles/smokey", pos1 + math.Rand(2, 14) * SCALE * VectorRand():Normalize())
		particle:SetDieTime(math.Rand(1.6, 2))
		particle:SetStartAlpha(120)
		particle:SetEndAlpha(0)
		particle:SetStartSize(10 * SCALE)
		particle:SetEndSize(math.Rand(20, 28) * SCALE)
		particle:SetRoll(math.Rand(0, 360))
		particle:SetRollDelta(math.Rand(-2.8, 2.8))
		particle:SetColor(45, 45, 45)
	end
end
